package com.lsh.util;

import com.lsh.model.Student;

import java.io.*;

/**
 * @author ：LiuShihao
 * @date ：Created in 2020/12/15 5:55 下午
 * @desc ：
 */
public class SerializableUtil {
    public static void main(String[] args) throws Exception {
        serializ();
        deserializ();
    }
    
    public static void serializ() throws IOException {
        // 创建Student对象  这里使用了 lombok 中的建造者模式构建对象
        Student student = Student.builder().name("LiuShihao").age(18).score(100).build();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("student.txt")));
        objectOutputStream.writeObject(student);
        objectOutputStream.close();
        System.out.println("序列化成功！ 已生成student.txt文件");
    }

    public static void deserializ() throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("student.txt")));
        Student student = (Student) objectInputStream.readObject();
        System.out.println("==================================");
        System.out.println("反序列化结果：");
        System.out.println(student);
    }

}
